#include <stdio.h>
#ifndef bool
#define bool int
#endif

#ifndef _AST_HEADER_
#define _AST_HEADER_
#include "../include/ast.h"
#endif

#ifndef true
#define true 1
#endif

#ifndef false
#define false 0
#endif

#ifndef error
#define error -1
#endif


#ifndef INT
#define INT 0
#endif

#ifndef CHAR
#define CHAR 1
#endif

#ifndef DOUBLE
#define DOUBLE 2
#endif

#define INDEX_NODE 1
#define DATA_NODE 3

//typedef struct{
//    char table_name[20];
//    char table_key[20];
//}ForeignKeyStruct;

typedef struct key{
    char name[20];
    int flag;
    /*
    primary Key     0000 0001
    auto_increment  0000 0010
    not null        0000 0100
    unique          0000 1000
    foreign         0001 0000
    */
//    int restrain;
    int length;
//    ForeignKeyStruct foreign_key;
}KeyStruct,*KeyStructPoint;


typedef struct FileHeader{
    int headerLength;
    int keyNumber;
    int recordSize;
    int recordNumber;
    int firstAvailableSpace;
}FileHeaderStruct,*FileHeaderStructPoint;

typedef struct DataItemHeader{
    int freeSpaceIndex;
}DataItemHeaderStruct,DataItemHeaderStructPoint;


typedef struct ModifyResult{
    bool success;
    int modifyNumber;
}ModifyResultStruct,* ModifyResultStructPoint;

typedef struct SelectResultItem{
    void * space;
    struct SelectResultItem * next;
}* SelectResultItemStructPoint;

typedef struct SelectResult{
    int number;
    bool success;
    SelectResultItemStructPoint data;
}SelectResultStruct,* SelectResultStructPoint;

void idb_readKeyStruct(FILE * fp,int number,KeyStructPoint keys);

FileHeaderStructPoint idb_getFileHeader(FILE * fp);

FILE * getFileByTablePath(char * tablePath);

bool idb_create(FILE * fp,CreateFieldStructPoint);

SelectResultStructPoint idb_select_single_table(FILE * fp,SelectStructPoint ssp);

bool idb_insert(FILE * fp,InsertStructPoint isp);

ModifyResultStructPoint idb_delete(FILE * fp,DeleteStructPoint deleteStructPoint);

ModifyResultStructPoint idb_update(FILE * fp,UpdateStructPoint usp);